var timer_running = false;
function ajax_fun(url,id,loadid)
{
	var ajax_obj;
	if (window.XMLHttpRequest)
	{
	// code for IE7+, Firefox, Chrome, Opera, Safari
	ajax_obj=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	// code for IE6, IE5
	ajax_obj=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support XMLHTTP!");
	}
	ajax_obj.onreadystatechange=function()
	{
		if(ajax_obj.readyState==4)
		{
			document.getElementById(id).innerHTML=ajax_obj.responseText;
			custom_form(document.add_event.event_type);
			$(function() {




		custom_form(document.add_event.event_type);
	  repeat_form(document.add_event.repeated_type);
	  is_recurrent_active();
	  //$("#add_event").validate();
	  $('#is_event_recursive').change(function() {
	  if($('#is_event_recursive').val()!=0)
	  {
	  	$('#repeat_end_type').attr('disabled', '');
	  	$('#repeat_end_type').addClass('required');
	  }
	  else
	  {
	  	$('#repeat_end_type').attr('disabled', true);
	  	$('#repeat_end_type').removeClass('required');
	  }
		
	});
	  var validator = $("#add_event").bind("invalid-form.validate", function() {}).validate({
	  
	  errorPlacement: function(error, element) {
				error.appendTo( element.parent("div").next("div") );
			}
	  
	  });
	  
	  $( "#only_date" ).datepicker({minDate:0,
			dateFormat: 'M d yy',changeMonth: true,
			changeYear: true,
			showMonthAfterYear: true});
	  
	  var dates = $( "#fromDate, #toDate" ).datepicker({
			changeMonth: true,
			changeYear: true,
			showMonthAfterYear: true,
			minDate:0,
			dateFormat: 'M d yy',
			onSelect: function( selectedDate ) {
				var option = this.id == "fromDate" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
				validate_end_time(document.getElementById('fromTime').value , document.add_event.end_time,'');
			}
		});

 
});
document.getElementById(loadid).innerHTML='';
		}
		if(ajax_obj.readyState!=4)
		{
			document.getElementById(loadid).innerHTML='<img style="margin:0px;" src="/images/loading.gif" alt="Loading...">';
		}
	}
	ajax_obj.open("GET",url,true);
	ajax_obj.send(null);
}
function ajax_fun_hidden(url,id,time_stamp)
{
	var ajax_obj;
	var flashdata;
	if (window.XMLHttpRequest)
	{
	// code for IE7+, Firefox, Chrome, Opera, Safari
	ajax_obj=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	// code for IE6, IE5
	ajax_obj=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support XMLHTTP!");
	}
	ajax_obj.onreadystatechange=function()
	{
		if(ajax_obj.readyState==4)
		{
			jsonObject = ajax_obj.responseText;
			var validjson = jsonObject.split('-_-');//we can activate debug mode also
			
			//jsonObject = '{"0":{"id":"637","status":"2"},"1":{"id":"633","status":"2"},"length":2}';

			var key;
			var active_flashes=0;
			var json_array = eval('(' + validjson[0] + ')');
			var content;
			for (var i=0;i < json_array.length;i++)
			{
				key = json_array[i].id;
				switch(json_array[i].status)
					{
						case '0':
						{
							//pop up should be inactive
							$('#' + key ).hide();
							break;
						}
						case '1':
						{
							
							content = json_array[i].title + ' from ' + json_array[i].start_time +' to ' +  json_array[i].end_time;
							//pop up should be active
							if(document.getElementById(key))
							{
								$('#' + key ).remove();
							}
							printFlash_event(content,'',key);
							active_flashes++;
							break;
						}
						case '2':
						{
							break;
						
						}
						default:
						{
							break;
						}
					}
			
			}
			
			//{"637":{"id":"637","status":"2"},"633":{"id":"633","status":"1"}}
			if(active_flashes == 0)
			{
				$('#eventflashes').hide();
			}
			else
			{
				$('#eventflashes').show();
			}
			
		}

	}
	url = url + '&time=' + time_stamp;
	ajax_obj.open("GET",url,true);
	ajax_obj.send(null);
}
function ajax_fun_close(url,id,time_stamp,event_div_id)
{
	var ajax_obj;
	var flashdata;
	if (window.XMLHttpRequest)
	{
	// code for IE7+, Firefox, Chrome, Opera, Safari
	ajax_obj=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	// code for IE6, IE5
	ajax_obj=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support XMLHTTP!");
	}
	url = url + '&time=' + time_stamp + '&event_id='+event_div_id;
	ajax_obj.onreadystatechange=function()
	{
		if(ajax_obj.readyState==4)
		{
			$('#' + event_div_id).remove();
			if(timer_running==false)
			{
				timer = setInterval ( "check_reminders()", (5*1000) );
				timer_running = true;
			}
		}

	}
	ajax_obj.open("GET",url,true);
	ajax_obj.send(null);
}
function printFlash_event(msg, classname,id) {
	$('#flashesevemain').append('<div class="flash ' + classname + '" id="'+ id +'">' + msg + '</div>');
}
function ajax_fun_game(url,id,gameid,param,moveid,off_set)
{
	//alert(url+','+id+','+gameid+','+param+','+moveid+','+off_set);
	var ajax_obj;
	if (window.XMLHttpRequest)
	{
	// code for IE7+, Firefox, Chrome, Opera, Safari
	ajax_obj=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	// code for IE6, IE5
	ajax_obj=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support XMLHTTP!");
	}
	ajax_obj.onreadystatechange=function()
	{
		if(ajax_obj.readyState==4)
		{
			document.getElementById(id).innerHTML=ajax_obj.responseText;
			//document.getElementById(loadid).innerHTML='';
		}
		/*if(ajax_obj.readyState!=4)
		{
			document.getElementById(loadid).innerHTML='<img style="margin:0px;" src="images/loading.gif" alt="Loading...">';
		}*/
	}
	url = url + '?game_id=' + gameid + '&param=' + param + '&moveid=' + moveid + '&off_set=' + off_set;
	//alert(url);
	ajax_obj.open("GET",url,true);
	ajax_obj.send(null);
}
